<?php
include_once "xAP-functions/webincludes.php";

// build search list
$id=intval($_GET["id"]);
$date=intval($_GET["dt"]);
if ($id>0){
	if($date==0){
		$date=strtotime(date("Y-m-d"));
			echo "<script>
		$(document).ready(function(){
			LoadCCTVData(".$id.",0,0);
		});
		</script>";
		}else{

		}
	//build todat ref
	for($i=0;$i<24;$i++){
		$today[$i]["count"]=0;
	}
	// add in stats
	$sql="SELECT count(itemid) as cid,hour(Createddatestamp) as hr FROM xap_pictures_item
	where createddatestamp between '".date("Y-m-d",$date)." 00:00:00' and '".date("Y-m-d",$date)." 23:59:59' and  SourceID=".$id."
	group by hour(Createddatestamp)";
	$results=dbquery($sql);
	foreach($results as $v){
		$today[$v["hr"]]["count"]=$v["cid"];
	}		
		
		
	for($i=0;$i<24;$i++){
		
		$st=strtotime(date("Y-m-d",$date)." ".str_pad($i,2,"0",STR_PAD_LEFT).":00:00");
		if($i==23){
			$en=strtotime(date("Y-m-d",$date)." 23:59:59");
			}else{
		$en=strtotime(date("Y-m-d",$date)." ".str_pad(($i+1),2,"0",STR_PAD_LEFT).":00:00");
			}
		if(intval($today[$i]["count"])>0){
		$today[$i]["name"]="<a href='#' OnClick='LoadCCTVData(".$id.",".$st.",".$en.")'>".str_pad($i,2,"0",STR_PAD_LEFT).":00</a><BR>";	
			}else{
		$today[$i]["name"]="".str_pad($i,2,"0",STR_PAD_LEFT).":00<BR>";
			}
	}
	$today[24]["name"]="<a href='#' OnClick='LoadCCTV(".$id.")'>Now</a><BR>";
	

	
}
echo  "";
?>

<table width=100%><TR><TD width=100 class='normaltext' valign=top nowrap>
<a href='#'><B><?=date("Y-m-d",$date);?></B></a><BR>
<a href='#' OnClick="LoadCCTV(<?=$id?>,<?=strtotime(date("Y-m-d",$date)."- 1 day")?>)" >&lt;<?=date("jS",strtotime(date("Y-m-d",$date)."- 1 day"));?></a>
<a href='#' OnClick="LoadCCTV(<?=$id?>,<?=strtotime(date("Y-m-d",$date)."+ 1 day")?>)"><?=date("jS",strtotime(date("Y-m-d",$date)."+ 1 day"));?>&gt;</a>
<BR>
<?foreach($today as $v):?>
<?=$v["name"]?>
<?endforeach;?>
</td><TD valign=top>
<div id='cctvimages' class='boldtext'></div>
</TD>
</TR>
</table>